
#include <input_manager.h>
#include <tslib.h>
#include <stdio.h>

//函数内部使用   加static
static struct tsdev *g_ts; //  g_ts相当于是触摸屏   <tslib.h>里面的

static int TouchscreenGetInputEvent(PInputEvent ptInputEvent)
{
	struct ts_sample samp;
	int ret;
	//wz-0820:ts_read是tslib库中的API，读取g_ts设备中的数据放到samp结构体中，后面再传给ptInputEvent。
	ret = ts_read(g_ts, &samp, 1);  //ts_read 读数据，支持单点触摸就行，不用加  _mt
	
	if (ret != 1)
		return -1;

	ptInputEvent->iType     = INPUT_TYPE_TOUCH;
	ptInputEvent->iX        = samp.x;
	ptInputEvent->iY        = samp.y;
	ptInputEvent->iPressure = samp.pressure;
	ptInputEvent->tTime     = samp.tv;

	return 0;
}

static int TouchscreenDeviceInit(void)
{
	g_ts = ts_setup(NULL, 0);
	if (!g_ts)
	{
		printf("ts_setup err\n");
		return -1;
	}
	return 0;
}

static int TouchscreenDeviceExit(void)
{
	ts_close(g_ts);
	return 0;
}

//3、给触摸屏构造这个结构体，来实现这三个函数
static InputDevice g_tTouchscreenDev ={
	.name = "touchscreen",
	.GetInputEvent  = TouchscreenGetInputEvent,
	.DeviceInit     = TouchscreenDeviceInit,
	.DeviceExit     = TouchscreenDeviceExit,
};

void TouchscreenRegister(void)//
{
	RegisterInputDevice(&g_tTouchscreenDev);
}


#if 0
//阶段测试
int main(int argc, char **argv)
{
	InputEvent event;  //InputEvent这是个啥结构体
	int ret;
	g_tTouchscreenDev.DeviceInit();
	while (1)
	{
		ret = g_tTouchscreenDev.GetInputEvent(&event); //设备初始化，正常初始化后返回值为0.
		if (ret){
			printf("GetInputEvent err!\n");
			return -1;
		}
		else
		{
			printf("Type      : %d\n", event.iType);
			printf("iX        : %d\n", event.iX);
			printf("iY        : %d\n", event.iY);
			printf("iPressure : %d\n", event.iPressure);
		}
	}
}

#endif




